<?php


namespace app\system\controller;
use think\Controller;
use think\response\Json;
use think\Request;

class Login extends Controller
{
    /**
     * @Notes 用户登录页面
     * @return Json
     * @date 2021/06
     * @time 17:00
     * @author wm
     */
      public function index() {
          if(request()->isPost()) {
              $data = input('post.');
              if(!captcha_check($data['captcha'])) return ajaxArray(0,'验证码错误');
              $user = model('admin')->get(['username'=>$data['username'],'password'=>md5($data['password'])]);
              if($user) {
                  $user->last_login_time = time();
                  $user->last_login_ip = request()->ip();
                  $user->save();
                  session('username',$user['username']);
                  session('aid',$user['id']);
                  return ajaxArray(1,'登录成功');
              }
              return ajaxArray(0, '用户名或者密码错误，重新输入!');
          }else {
              session('aid',null);
              $this->assign('system_name', web_config('name') . '后台登录');
              $this->assign('name', web_config('name'));
              return $this->fetch();
          }
      }
      //退出登录
      public function outlogin() {
        session('aid',null);
        // $this->redirect('Login/index');
        return ajaxArray(1,'已退出');
      }
}